package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Allorder;
import com.wn.class30.entity.Business;
import com.wn.class30.entity.Myorder;
import com.wn.class30.service.*;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/houtai/page/allOrder/")
@RestController
@Slf4j
public class AllOrderController {

    @Resource
    private AllOrderService allOrderService;
    @Resource
    private OrderService orderService;
    @Resource
    private Couponservice couponservice;
    @Resource
    private UserAddressService userAddressService;
    @Resource
    private UserService userService;
    @Resource
    private BusinessService businessService;

    @RequestMapping("list")
    public Result selectAllOrders(Integer limit,Integer page,String allorderNumber,String startDate,String endDate,String username)throws Exception{
        log.info("allorderNumber======="+allorderNumber);
        log.info("startDate========"+startDate);
        log.info("endDate========"+endDate);
        log.info("endDate========"+username);
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf((page-1)*limit) );
        map.put("limit",String.valueOf(limit) );
        map.put("allorderNumber", allorderNumber);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("username",username);
        Long orderCount = allOrderService.getAllOrderCount(map);
        Page p = new Page();
        p.setCurrentPage((page-1)*limit);
        p.setTotalSize(orderCount);
        p.setPageSize(limit);
        long pageSize = orderCount % limit == 0? orderCount / limit:orderCount / limit + 1;
        p.setPageSize((int)pageSize);
        List<Allorder> allorders = allOrderService.selectAllorders(map);
        for (Allorder allorder : allorders) {
            allorder.setMyorders(orderService.selectMyOrderByAllOrderId(allorder.getAllorderId()));
            allorder.setCupon(couponservice.selectCuponById(allorder.getUserCuponId()));
            allorder.setUser(userService.queryUserByUderid(allorder.getUserId()));
        }
        return new Result("0","success",p,allorders);
    }

    @RequestMapping("selectAllOrderById")
    public Result selectAllOrderById(Integer allOrderId)throws Exception{
        if(allOrderId != null){
            Allorder allorder = allOrderService.selectAllOrderById(allOrderId);
            allorder.setCupon(couponservice.selectCuponById(allorder.getUserCuponId()));
            allorder.setUser(userService.queryUserByUderid(allorder.getUserId()));
            allorder.setUserAddress(userAddressService.selectUserAddressById(allorder.getUserAddressId()));
            List<Myorder> myorders = orderService.selectMyOrderByAllOrderId(allOrderId);
            for (Myorder myorder : myorders) {
                myorder.setBusiness(businessService.selectBusinessById(myorder.getBussinessId()));
            }
            allorder.setMyorders(myorders);
            return new Result("200","success",allorder,null);
        }else{
            return new Result("500","error",null,null);
        }

    }
}
